package com.fulihui.usercore.core.repository.impl;

import com.fulihui.usercore.biz.conv.CustomerConv;
import com.fulihui.usercore.core.generator.IGenerator;
import com.fulihui.usercore.core.repository.CustomerRepositoery;
import com.fulihui.usercore.dal.dataobj.CustomerDO;
import com.fulihui.usercore.dal.mapper.CustomerMapper;
import com.fulihui.usercore.dto.CustomerDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Willard.Hu on 2016/11/9 0009.
 */
@Repository
public class CustomerRepositoeryImpl implements CustomerRepositoery {
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private IGenerator     customerIdGenerator;
    @Autowired
    private CustomerConv   customerConv;

    @Override
    public String create(CustomerDTO customerDTO) {
        String id = customerIdGenerator.generate();
        customerDTO.setCustomerId(id);
        customerMapper.insert(customerConv.toDO(customerDTO));
        return id;
    }

    @Override
    public boolean modify(CustomerDTO customerDTO) {
        return customerMapper.update(customerConv.toDO(customerDTO));
    }

    @Override
    public CustomerDTO queryByPK(String customerId) {
        CustomerDO customerDO = customerMapper.queryByPK(customerId);
        return customerConv.toDTO(customerDO);
    }

    @Override
    public List<CustomerDTO> query(CustomerDTO condition) {
        List<CustomerDO> list = customerMapper.query(customerConv.toDO(condition));
        return customerConv.toList(list);
    }
}
